const puppeteer = require('puppeteer');
const data = require('./data.json');
console.log(data);

(async () => {

    for (const account of data.account) {
        // 启动浏览器
        const browser = await puppeteer.launch({ headless: false, args: ['--start-maximized'] }); // 设置 headless: false 以便看到浏览器操作
        const page = await browser.newPage();
 
        // 设置浏览器窗口大小
        const { width, height } = await page.evaluate(() => {
            return {
                width: window.screen.availWidth,
                height: window.screen.availHeight
            };
        });
        await page.setViewport({ width, height });

        // 打开指定的 URL
        await page.goto('http://localhost:8010/apps/xunxin/login');

        // 等待页面加载完成并填写表单
        await page.waitForSelector('#basic_passId');
        await page.type('#basic_passId', account.username);
        await page.type('#basic_pwd', account.password);

        // 获取文字为“登录”的div元素并点击
        await page.evaluate(() => {
            const loginButton = Array.from(document.querySelectorAll('div')).find(el => el.textContent.trim() === '登 录');
            if (loginButton) {
                loginButton.click();
            }
        });

        // 等待页面导航完成
        await page.waitForNavigation();
        // 增加等待时间，确保页面完全渲染
        await new Promise(resolve => setTimeout(resolve, 500));

        // 获取所有的.ant-ribbon-text元素的文本内容
        const hostFound = await page.evaluate(() => {
            const roleItem = Array.from(document.querySelectorAll('.ant-ribbon-wrapper'));
            if (roleItem.length > 0) {
                for (let i = 0; i < roleItem.length; i++) {
                    const host = Array.from(roleItem[i].children).find(el => el.textContent.trim().includes('主持人'));
                    if (host) {
                        host.click();
                        return true;
                    }
                }
            }
            return false;
        });

        if (!hostFound) {
            console.log(`未找到“主持人”按钮，账号: ${account.username}`);
        }

       
      const submitButton =  await page.evaluate(() => {
            const confirmButton = Array.from(document.querySelectorAll('button')).find(el => el.textContent.trim() === '确 认');
            if (confirmButton) {
                confirmButton.click();
                return true
            }
        });
        if (!submitButton) {
            console.log(`未找到“确认”按钮，账号: ${account.username}`);
        }


        // 增加等待时间，确保页面完全渲染
        await new Promise(resolve => setTimeout(resolve, 500));
        const room = await page.evaluate((name) => {
            const roomItem = Array.from(document.querySelectorAll('[class*="rooms-card"]'));
            for (let i = 0; i < roomItem.length; i++) {
                const room =( Array.from(roomItem[i].children))[0];
                const roomLeft = Array.from(room.children)[0];
                const roomRight = Array.from(room.children)[1];
                const roomName = roomLeft.children[0].textContent.trim();
                console.log(roomName);
                console.log(name);
                
                if ('游戏中' + name === roomName) {
                    roomRight.click();
                    return true
                }
            }
        }, data.roomName)
        if (!room) {
            console.log(`未找到“${data.roomName}”房间，账号: ${account.username}`);
        }
  

        // 关闭页面
        // await page.close();
    }

    // // 关闭浏览器
    // await browser.close();
})();